﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Content;
using System.Diagnostics;

namespace Homebound
{
    class GameOverState : State
    {
        SpriteFont font;
        SpriteFont keyFont;
        Texture2D background;
        bool nextEnabled;
        public bool reset;

        public GameOverState(GraphicsDeviceManager newGraphics, GameServiceContainer newServices, ContentManager newContent)
            : base(newGraphics, newServices, newContent)
        {
        }

        // Load the game's external content.
        public override void loadContent()
        {
            // Load overlay textures
            font = content.Load<SpriteFont>("Fonts/Choice");
            keyFont = content.Load<SpriteFont>("Fonts/Hud");
            background = content.Load<Texture2D>("nebula_1");
            base.loadContent();
        }

        // Called whenever a new state is selected.
        public override void onEnter(State state)
        {
            loadContent();
            reset = false;
            nextEnabled = true;
            enterPressed = false;
            base.onEnter(state);
        }

        // Called when the state is changing.
        public override void onExit(State state)
        {
            base.onExit(state);
        }

        // Draw the visual representation of the state.
        public override void draw(SpriteBatch spriteBatch, GameTime gameTime, Vector2 screenSize)
        {
            spriteBatch.Begin();
                spriteBatch.Draw(background, new Vector2(0, 0), Color.White);
                spriteBatch.DrawString(font, "GAME OVER", new Vector2(screenSize.X / 2, screenSize.Y / 2), Color.White);
                spriteBatch.DrawString(keyFont, "Press Right Shift or Left Bumper to Restart.", new Vector2(screenSize.X / 3, 0.75f * screenSize.Y / 2), Color.White);
            spriteBatch.End();
            base.draw(spriteBatch, gameTime, screenSize);
        }

        // Handles all input for the current state, (Keyboard, GamePad).
        public override void input(GameTime gameTime, List<Keys> keys, GamePadButtons buttons, GamePadDPad dpad, GamePadThumbSticks thumbSticks)
        {
            // Return to the title screen when enter is pressed.
            if ((Keyboard.GetState().IsKeyDown(Keys.RightShift) || GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.LeftShoulder)) && nextEnabled)
            {
                reset = true;
                nextEnabled = false;
            }

            if ((Keyboard.GetState().IsKeyUp(Keys.RightShift) && GamePad.GetState(PlayerIndex.One).IsButtonUp(Buttons.LeftShoulder)))
                nextEnabled = true;
        }

        // Handles all update operations for the current state, (Keyboard, GamePad).
        public override void update(GameTime gameTime)
        {
            base.update(gameTime);
        }

        // Conditions necessary to enter go return to a next state.
        public override bool goingForward(State prev)
        {
            GamePlayState gamePlayState = (GamePlayState)prev;
            return (gamePlayState.level.player.lives < 0);
        }
    }
}
